package com.kgc.scd.service.impl;

import com.kgc.scd.bean.*;
import com.kgc.scd.mapper.CartMapper;
import com.kgc.scd.mapper.OrderItemMapper;
import com.kgc.scd.mapper.OrderMapper;
import com.kgc.scd.service.SubmitOrderService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 * Created On : 2022/6/14.
 * <p>
 * Author     : 98730
 * <p>
 * Description: PlaceOrderServiceImpl
 */
@Service
@Slf4j
public class SubmitOrderServiceImpl implements SubmitOrderService {

    @Autowired(required = false)
    private CartMapper cartMapper;

    @Autowired(required = false)
    private OrderItemMapper orderItemMapper;

    @Autowired(required = false)
    private OrderMapper orderMapper;

    @Override
    public boolean addOrderItem(List<Integer> cartIds, String orderId) {
        for (Integer cartId : cartIds){
            Cart cart = cartMapper.selectByPrimaryKey(cartId);

            OrderItem orderItem = OrderItem.builder()
                    .itemOrderid(orderId)
                    .itemProid(cart.getCartProid())
                    .itemNum(cart.getCartNum())
                    .build();
            orderItemMapper.insertSelective(orderItem);
        }
        return true;
    }

    @Override
    public boolean addOrder(List<Integer> cartIds, String orderId) {
        Cart cart = cartMapper.selectByPrimaryKey(cartIds.get(0));
        log.info("cart:{}",cart);
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

        Order order = Order.builder()
                .orderId(orderId)
                .orderUid(cart.getCartUid())
                .build();
        log.info("order:{}",order);


        return orderMapper.insert(order) > 0;
    }

    @Override
    public List<OrderItemResult> selectOrderItemResult(String orderId) {
        return orderItemMapper.selectOrderItemResult(orderId);
    }

    @Override
    public List<SelectSeckillOrder> selectSeckillOrderResult(String orderId) {
        return orderItemMapper.selectSeckillOrderResult(orderId);
    }
}
